package com.core.exception;

import com.core.model.PageData;
import com.core.response.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    private static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    /**
     * 所有异常报错
     * @param request
     * @param exception
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value=Exception.class)
    public ResponseResult allExceptionHandler(HttpServletRequest request,
                                              Exception exception) throws Exception
    {
        System.out.println(request);
        PageData pageData = new PageData(request);
        System.out.println(pageData);
        logger.error("====================================================开始打印异常===============================================================");
        exception.printStackTrace();
        logger.error(exception.getLocalizedMessage());
        logger.error(String.valueOf(exception.getCause()));
        logger.error(String.valueOf(exception.getSuppressed()));
        logger.error(exception.getMessage());
        logger.error(String.valueOf(exception.getClass().getAnnotatedSuperclass()));
        logger.error(String.valueOf(exception.getStackTrace()));
        logger.error("======================================================end=====================================================================");


        return ResponseResult.error("服务器发生异常，请联系管理员",100);
    }





}
